<template>
  <div class="choice-commidy">
    <div class="already-content">
      <EditTable
        ref="editTable"
        @onChangeSelectAll="onSelectAll"
        @onJoinSale="onJoinSale"
        @onJoinSaleCheck="onJoinSaleCheck"
        @onShowChild="onShowChild"
        @onSetDiscount="onSetDiscount"
        @onSetReduce="onSetReduce"
        @onSetSale="onSetSale"
        @onSetQuantity="onSetQuantity"
        @onSetsellOutQuantity="onSetsellOutQuantity"
        :listData="columns1"
        :status="status"
        :list="$store.state.singleSale.saleData"
      ></EditTable>
    </div>
  </div>
</template>
<script>
import {mapGetters} from "vuex"
import EditTable from "../edit/ChoiceTable";
import { getValueArray, setEmptyArray, getSelectStatus, getValueStatus, columns1, data1 } from "./ChoiceCommidyMethods"
import {onSet, compareSaleGift, getCode, isBigSix, setArrayValue, setAdd, setExchangeValue, onJoinSaleBody, isPriceEmpty, isBigSixItem, isPriceEmptyItem, onSetPromotion} from "@/views/sales/common/TableSaleMethods";
export default {
  name: "ChoiceCommidy",
  components: {
    EditTable
  },
  created() {
    if (!this.$store.state.singleSale.saleData.length && !this.$route.query.id) {
      this.getProductData({size: 5000});
    }
  },
  data() {
    return {
      model1: "",
      value: "",
      cityList: [],
      columns1,
      data1,
      status: "pass"
    };
  },
  methods: {
    onShowChild(item) {
      item.showChildren = !item.showChildren;
    },
    getProductData(param) {
      let self = this;
      self.status = "loading";
      this.$ajaxGet("/activity/product/spu_sku_page", param).then(
        data => {
          self.$store.state.singleSale.saleData = self.data1 = data.records;
          setAdd(self, self.data1, {index: "checked",val1: false,childName: "productSkuList"})
          setAdd(self, self.data1, {index: "buttonChecked",val1: false,childName: "productSkuList"})
          setAdd(self, self.data1, {index: "showChildren",val1: false,childName: "productSkuList"})
          setAdd(self, self.data1, {index: "discount",val1: "",childName: "productSkuList"})
          setAdd(self, self.data1, {index: "reduce",val1: "",childName: "productSkuList"})
          setAdd(self, self.data1, {index: "sale",val1: "",childName: "productSkuList"})
          setAdd(self, self.data1, {index: 'limitQuantity',val1: '',childName: "productSkuList"})
          setAdd(self, self.data1, {index: 'sellOutQuantity', val1: '', childName: "productSkuList"})
          // getCode 的值
          getCode(self, self.data1, {index: "parentcode",childName: "productSkuList"})
          this.status = "pass"
          compareSaleGift(self.$store.state.singleSale.saleData, self.$store.state.singleSale.GetsaleData, 'sale', this.onSetSale, 'limitQuantity', 'sellOutQuantity')
        }
      );
    },
    getSaleData(type, data) {
      if (type == "buttonChecked") {
      } else {
        if (isPriceEmpty(this.$store.state.singleSale.saleData, {index: "sale", index1: "checked",childName: "productSkuList"})) {
          this.$Message.error("促销价格不能为空")
          return false
        }
        if (isBigSix(this.$store.state.singleSale.saleData, {index: "discount",index1: "checked", childName: "productSkuList"})) {
          this.$Message.error("已选商品不能小于6折")
          return false
        }
        if(isPriceEmpty(this.$store.state.singleSale.saleData, {index: "sellOutQuantity", index1: "checked",childName: "productSkuList"})) {
          if(this.isShowDrop) {
            this.$Message.error("促销库存不能为空")
            return false
          }
        }
      }
      this.$store.commit("getSingleData", type)
    },
    onSelectAll(param) {
      //点击选中所有checked按钮的时候出发的事件
      param ? setArrayValue(this.$store.state.singleSale.saleData, {index: "checked", val1: true, childName: "productSkuList"}) : setArrayValue(this.$store.state.singleSale.saleData, { index: "checked", val1: false, childName: "productSkuList"})
    },
    onJoinSaleBody(item, todo, params) {
      onJoinSaleBody(item, todo, params);
    },
    onSetbatch(param) {
      if (!/^([\+ \-]?([1-9]\d*)|(0))([.]\d{0,2})?$/.test(param.val)) {
        return false;
      }
      let object = {};
      let self = this;
      if (param.index == 1) {
        //批量折扣
        object = { index: "discount", val: param.val };
        if (param.val < 6 || param.val > 10) {
          return false;
        }
      } else if (param.index == 2) {
        //批量减
        if (param.val < 0) {
          return false;
        }
        object = { index: "reduce", val: param.val };
      } else if (param.index == 3) {
        //批量促销
        object = { index: "sale", val: param.val };
      }
      for (let item of self.$store.state.singleSale.saleData) {
        for (let todo of item.productSkuList) {
          if (todo.checked) {
            todo[object.index] = object.val;
            if (param.index == 1) {
              //折扣
              item.discount = object.val;
              todo.reduce = (
                todo.salePrice -
                todo.discount * todo.salePrice * 0.1
              ).toFixed(2);
              todo.sale = (todo.discount * todo.salePrice * 0.1).toFixed(2);
            } else if (param.index == 2) {
              //减
              item.reduce = object.val;
              todo.discount = (
                ((todo.salePrice - todo.reduce) / todo.salePrice) *
                10
              ).toFixed(2);
              todo.sale = todo.salePrice - todo.reduce;
            } else if (param.index == 3) {
              //促销
              item.sale = object.val;
              todo.discount = ((todo.sale / todo.salePrice) * 10).toFixed(2);
              todo.reduce = todo.salePrice - todo.sale;
            }
          }
        }
        if (param.index == 1 && item.checked) {
          item.reduce = `${this.getMinValue(item.productSkuList, {
            index: "reduce"
          })}-${this.getMaxValue(item.productSkuList, {
            index: "reduce"
          })}`;
          item.sale = `${this.getMinValue(item.productSkuList, {
            index: "sale"
          })}-${this.getMaxValue(item.productSkuList, {
            index: "sale"
          })}`;
        } else if (param.index == 2 && item.checked) {
          item.discount = `${this.getMinValue(item.productSkuList, {
            index: "discount"
          }) || 0}-${this.getMaxValue(item.productSkuList, {
            index: "discount"
          })}`;
          item.sale = `${this.getMinValue(item.productSkuList, {
            index: "sale"
          }) || 0}-${this.getMaxValue(item.productSkuList, {
            index: "sale"
          })}`;
        } else if (param.index == 3 && item.checked) {
          item.discount = `${this.getMinValue(item.productSkuList, {
            index: "discount"
          }) || 0}-${this.getMaxValue(item.productSkuList, {
            index: "discount"
          })}`;
          item.reduce = `${this.getMinValue(item.productSkuList, {
            index: "reduce"
          }) || 0}-${this.getMaxValue(item.productSkuList, {
            index: "reduce"
          })}`;
        }
      }
    },
    onJoinSale(item, todo, selectAll) {
      //点击加入促销的时候触发的事件
      if (isPriceEmptyItem(item, {index: "discount",childName: "productSkuList"})) {
        this.$Message.error("促销价格不能为空");
        return false;
      }
      if (isBigSixItem(item, { index: "discount", childName: "productSkuList" })) {
        this.$Message.error("已选商品不能小于6折");
        return false;
      }
      if(isPriceEmptyItem(item, {index: "sellOutQuantity",childName: "productSkuList"})) {
        if(this.isShowDrop) {
          this.$Message.error("促销库存不能为空")
          return false
        }
      }
      item.buttonChecked = !item.buttonChecked
      this.onJoinSaleBody(item, todo, {
        index: "buttonChecked",
        index1: "productSkuList"
      })
      this.$store.commit("deleteSingleData", {todo: todo, item: item})
      this.getSaleData("buttonChecked", item)  
    },
    onJoinSaleCheck(item, todo, selectAll) {
      // 点击checked按钮的时候触发的事件
      this.onJoinSaleBody(item, todo, {index: "checked",index1: "productSkuList"})
    },
    getMaxValue(param, object) {
      for (let item of param) {
        let isTrue = true;
        for (let todo of param) {
          if (item[object.index] < todo[object.index]) {
            isTrue = false;
          }
        }
        if (isTrue) {
          return Number(item[object.index]).toFixed(2);
        }
      }
    },
    getMinValue(param, object) {
      for (let item of param) {
        let isTrue = true;
        for (let todo of param) {
          if (item[object.index] > todo[object.index]) {
            isTrue = false;
          }
        }
        if (isTrue) {
          return Number(item[object.index]).toFixed(2);
        }
      }
    },
    setChildPrice(param1, param2, object) {
      for (let item of param1[object.childName]) {
        item.discount = object.discountVal;
        item.reduce = (
          item.salePrice -
          item.discount * item.salePrice * 0.1
        ).toFixed(2);
        item.sale = (item.discount * item.salePrice * 0.1).toFixed(2);
      }
      param1[object.index2] = `${this.getMinValue(param1[object.childName], {
        index: "reduce"
      }) || 0}-${this.getMaxValue(param1[object.childName], {
        index: "reduce"
      })}`;
      param1[object.index3] = `${this.getMinValue(param1[object.childName], {
        index: "sale"
      }) || 0}-${this.getMaxValue(param1[object.childName], {
        index: "sale"
      })}`;
    },
    onSetDiscount(item, todo) {
      if (!item.discount || !/^([\+ \-]?([1-9]\d*)|(0))([.]\d{0,2})?$/.test(item.discount)) {
        return false;
      }
      //输入打折的时候控制
      if (item.productSkuList && item.productSkuList.length > 0) {
        this.setChildPrice(item, todo, {
          index1: "discount",
          index2: "reduce",
          index3: "sale",
          discountVal: item.discount,
          childName: "productSkuList"
        });
      } else if (!item.productSkuList) {
        item.reduce = (
          item.salePrice -
          item.discount * item.salePrice * 0.1
        ).toFixed(2);
        item.sale = (Number(item.discount) * Number(item.salePrice) * 0.1).toFixed(2);
        todo.discount = `${this.getMinValue(todo.productSkuList, {
          index: "discount"
        }) || 0}-${this.getMaxValue(todo.productSkuList, {
          index: "discount"
        })}`;
        todo.reduce = `${this.getMinValue(todo.productSkuList, {
          index: "reduce"
        }) || 0}-${this.getMaxValue(todo.productSkuList, {
          index: "reduce"
        })}`;
        todo.sale = `${this.getMinValue(todo.productSkuList, {
          index: "sale"
        }) || 0}-${this.getMaxValue(todo.productSkuList, {
          index: "sale"
        })}`;
      }
    },
    setChildReduce(param1, param2, object) {
      for (let item of param1[object.childName]) {
        item.reduce = object.reduceValue;
        item.discount = (
          ((item.salePrice - item.reduce) / item.salePrice) *
          10
        ).toFixed(2);
        item.sale = (item.salePrice - item.reduce).toFixed(2);
      }
      param1[object.index1] = `${this.getMinValue(param1[object.childName], {
        index: "discount"
      }) || 0}-${this.getMaxValue(param1[object.childName], {
        index: "discount"
      })}`;
      param1[object.index3] = `${this.getMinValue(param1[object.childName], {
        index: "sale"
      }) || 0}-${this.getMaxValue(param1[object.childName], {
        index: "sale"
      })}`;
    },
    onSetReduce(item, todo) {
      // 输入打折的时候控制
      if (!item.reduce || !/^([\+ \-]?([1-9]\d*)|(0))([.]\d{0,2})?$/.test(item.reduce)) {
        return false;
      }
      if (item.productSkuList && item.productSkuList.length > 0) {
        this.setChildReduce(item, todo, {
          index1: "discount",
          index2: "reduce",
          index3: "sale",
          reduceValue: item.reduce,
          childName: "productSkuList"
        });
      } else if (!item.productSkuList) {
        item.discount = (
          ((item.salePrice - item.reduce) / item.salePrice) *
          10
        ).toFixed(2);
        item.sale = (Number(item.salePrice) - Number(item.reduce)).toFixed(2);
        todo.discount = `${this.getMinValue(todo.productSkuList, {
          index: "discount"
        }) || 0}-${this.getMaxValue(todo.productSkuList, {
          index: "discount"
        })}`;
        todo.reduce = `${this.getMinValue(todo.productSkuList, {
          index: "reduce"
        }) || 0}-${this.getMaxValue(todo.productSkuList, {
          index: "reduce"
        })}`;
        todo.sale = `${this.getMinValue(todo.productSkuList, {
          index: "sale"
        }) || 0}-${this.getMaxValue(todo.productSkuList, {
          index: "sale"
        })}`;
      }
    },
    setChildSale(param1, param2, object) {
      for (let item of param1[object.childName]) {
        item.sale = Number(object.saleValue).toFixed(2);
        item.discount = ((object.saleValue / item.salePrice) * 10).toFixed(2);
        item.reduce = (item.salePrice - item.sale).toFixed(2);
      }
      param1[object.index1] = `${this.getMinValue(param1[object.childName], {
        index: "discount"
      }) || 0}-${this.getMaxValue(param1[object.childName], {
        index: "discount"
      })}`;
      param1[object.index2] = `${this.getMinValue(param1[object.childName], {
        index: "reduce"
      }) || 0}-${this.getMaxValue(param1[object.childName], {
        index: "reduce"
      })}`;
    },
    onSetSale(item, todo) {
      // 输入打折的时候控制
      if (!item.sale || !/^([\+ \-]?([1-9]\d*)|(0))([.]\d{0,2})?$/.test(item.sale)) {
        return false;
      }
      if (item.productSkuList && item.productSkuList.length > 0) {
        this.setChildSale(item, todo, {
          index1: "discount",
          index2: "reduce",
          index3: "sale",
          saleValue: item.sale,
          childName: "productSkuList"
        });
      } else if (!item.productSkuList) {
        !item.reduce ? item.reduce = 0 : ''
        item.discount = (
          ((Number(item.sale)) / Number(item.salePrice)) *
          10
        ).toFixed(2);
        item.reduce = (Number(item.salePrice) - Number(item.sale)).toFixed(2);
        todo.discount = `${this.getMinValue(todo.productSkuList, {
          index: "discount"
        }) || 0}-${this.getMaxValue(todo.productSkuList, {
          index: "discount"
        })}`;
        todo.reduce = `${this.getMinValue(todo.productSkuList, {
          index: "reduce"
        }) || 0}-${this.getMaxValue(todo.productSkuList, {
          index: "reduce"
        })}`;
        todo.sale = `${this.getMinValue(todo.productSkuList, {
          index: "sale"
        }) || 0}-${this.getMaxValue(todo.productSkuList, {
          index: "sale"
        })}`;
      }
    },
    setChildQuantity(data) {
      for (let item of data.productSkuList) {
        item.limitQuantity = Number(data.limitQuantity).toFixed(0)
      }
    },
    onSetQuantity(item, todo) {
      if(!item.limitQuantity || !/^([\+ \-]?([1-9]\d*)|(0))([.]\d{0,2})?$/.test(item.limitQuantity)) {
        return false;
      } 
      if (item.productSkuList && item.productSkuList.length > 0) {
        this.setChildQuantity(item)
      } else if (!item.productSkuList) {
        todo.limitQuantity =`${this.getMinValue(todo.productSkuList, {index: 'limitQuantity'}) || 0} - ${this.getMaxValue(todo.productSkuList, {index: "limitQuantity"})}`
      }
    },
    onSetsellOutQuantity(item, todo) {
      onSet(item, todo, 'sellOutQuantity')
    }
  },
  computed: {
    ...mapGetters([
      'isShowDrop'
    ])
  }
};
</script>

